<!DOCTYPE html>
<html lang='zh-ch'>

<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <title>江苏联通加速器套餐包</title>
  <link rel='stylesheet' href='css/style.css'>
</head>

<body>
<div class='main'>
  <div class='banner'>
    <img src='img/banner.png' alt='banner'>
  </div>
  <div class='phone'>
    <img class='bg' src='img/8.png' alt=''>
    <span class='more'>了解更多></span>
    <div class='form'>
      <div class='user'>
        <div class='row'>
          <h2 id='userPhone'>用户<span></span></h2>
          <p>您的包月订购情况: <span id='userStatus'></span></p>
          <p id='countStatus'>您本月还可以订购<span id='total'></span>次次包</p>
        </div>
        <div class='btn'>
          <img id='logout' src='img/11.png' alt='btn'>
        </div>
      </div>
      <div class='login'>
        <div class='phone-num'>
          <input placeholder='请输入手机号' type='text' maxlength='11'>
        </div>
        <div class='code-num'>
          <input placeholder='请输入验证码' type='text' maxlength='6'>
          <div class='get-code'>
            <img class='code-img' src='img/code.png' alt='code'>
            <span id='btn-code'>获取验证码</span>
          </div>
        </div>
        <div class='btn'>
          <img id='login' src='img/4.png' alt='btn'>
        </div>
      </div>
    </div>
  </div>
  <div class='info'>
    <img src='img/7.png' alt='info'>
  </div>
  <div class='actions'>
    <img id='m' src='img/5.png' alt=''>
    <img id='t' src='img/6.png' alt=''>
  </div>
  <div style='height: 30px;'></div>
  <div class='tips'>
    <div class='bg'></div>
    <div class='popup'>
      <img src='img/tips.png' alt='tips'>
      <span id='closeTag'></span>
    </div>
  </div>
</div>

<div class='note'>
  <img src='img/note.jpg' alt='note'>
  <span id='closeNote'></span>
</div>
<script src='js/zepto.min.js'></script>
<script src='js/dialog.js'></script>
<script>
  (function () {

    var phone = getUrlParam('telephone') || localStorage.getItem('phone') || '';
    var num = 60, tm = null;
    if (phone) {
      checkStatus(phone, true);
    } else {
      $('.user').hide();
      $('.login').show();
    }

    //检查用户状态
    function checkStatus(p, isLogin) {
      $.ajax({
        url: 'http://116.147.0.37:8080/qos/qosQuery',
        type: 'POST',
        contentType: 'application/json',
        xhrFields: {withCredentials: true},
        data: JSON.stringify({
          'phone': p,
          'city': '340',
          'district': '340',
          'productId': '90363852'
        }),
        success: function (data) {
          if (isLogin) {
            localStorage.setItem('phone', p);
            $('#userPhone span').html(p);
            $('#total').html(data.data ? data.data.count : '');
            $('.user').show();
            $('.login').hide();
            if (data && data.code === 200) {
              $('#userStatus').html('已订购');
              $('#countStatus').show();
            } else {
              $('#userStatus').html('未订购');
              $('#countStatus').hide();
            }
          } else {
            if (data && data.code === 200) {
              $('#userStatus').html('已订购');
              $('#userPhone span').html(p);
              $('#total').html(data.data ? data.data.count : '');
              localStorage.setItem('phone', p);
              $('.user').show();
              $('.login').hide();
            } else {
              $('.user').hide();
              $('.login').show();
              localStorage.clear();
              // showPopup(data ? data.msg : 'error')
            }
          }
        },
        error: function (err) {
          if (isLogin) {
            $('.user').show();
            $('.login').hide();
            if (err.status === 500) {
              showPopup(err ? err.responseText : 'error');
            } else {
              showPopup('出错了');
            }
          } else {
            $('.user').hide();
            $('.login').show();
            if (err.status === 500) {
              showPopup(err ? err.responseText : 'error');
            } else {
              showPopup('出错了');
            }
          }
        }
      });
    }

    //获取验证码
    function getCode(tel) {
      $.ajax({
        url: 'http://81.70.85.144:8383/user/sendSMS?telephone=' + tel,
        type: 'GET',
        xhrFields: {withCredentials: true},
        success: function (data) {
          if (data && data.code === 0) {
            showPopup('发送成功');
          } else {
            showPopup(data ? data.message : '发送失败');
          }
        },
        error: function (err) {
          showPopup(err ? err.message : '发送失败');
        }
      });
    }

    //校验手机号是否合法
    function isPhoneNum(tel) {
      var reg = /^((1)+\d{10})$/;
      if (!reg.test(tel)) {
        showPopup('请输入有效的手机号码！');
        return false;
      } else {
        return true;
      }
    }

    //校验验证码是否合法
    function isCode(c) {
      var regCode = /^(\d{6})$/;
      if (!regCode.test(c)) {
        showPopup('请输入有效的验证码！');
        return false;
      } else {
        return true;
      }
    }

    //订购次包
    function order(o) {
      $.ajax({
        url: 'http://116.147.0.37:8080/qos/qosOrder5',
        type: 'POST',
        contentType: 'application/json',
        xhrFields: {withCredentials: true},
        data: JSON.stringify({
          'phone': o,
          'city': '340',
          'district': '340',
          'productId': '90781176',
          'packageId': '52446290',
          'elementId': '63258405'
        }),
        success: function (data) {
          if (data && data.code === 200) {
            $(document).dialog({
              style: 'ios',
              titleText: '订购成功',
              content: '您本月还可以订购' + data.data.count + '次次包'
            });
          } else {
            showPopup(data ? data.msg : '订购失败');
          }
        },
        error: function (err) {
          showPopup(err ? err.message : '订购失败');
        }
      });
    }

    //登陆
    function login(p, c) {
      $.ajax({
        url: 'http://81.70.85.144:8383/user/loginByTelephone',
        type: 'POST',
        contentType: 'application/json',
        xhrFields: {withCredentials: true},
        data: JSON.stringify({
          'code': c,   // 验证码
          'telephone': p   // 手机号
        }),
        success: function (data) {
          if (data && data.code === 0) {
            localStorage.setItem('phone', p);
            checkStatus(p, true);
          } else {
            $('.login').show();
            $('.user').hide();
            showPopup(data ? data.message : '登陆失败');
          }
        },
        error: function (err) {
          $('.login').show();
          $('.user').hide();
          showPopup(err ? err.message : '登陆失败');
        }
      });
    }


    function order30(t) {
      $.ajax({
        url: 'http://116.147.0.37:8080/qos/qosOrder30',
        type: 'POST',
        contentType: 'application/json',
        xhrFields: {withCredentials: true},
        data: JSON.stringify({
          'phone': t   // 手机号
        }),
        success: function (data) {
          if (data && data.code === 200) {
            showPopup(data ? data.msg : '订购成功');
          } else {
            showPopup(data ? data.msg : '订购失败');
          }
        },
        error: function (err) {
          showPopup(err ? err.message : '订购失败');
        }
      });
    }

    function getUrlParam(name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    }

    //通用弹窗
    function showPopup(msg) {
      $(document).dialog({
        style: 'ios',
        content: msg
      });
    }

    $('#closeTag').on('click', function () {
      $('.tips').hide();
    });

    $('.actions #t').on('click', function () {
      var o = localStorage.getItem('phone') || '';
      if (o) {
        // order(o)
      } else {
        $('.tips').show();
      }
    });

    $('.actions #m').on('click', function () {
      var o = localStorage.getItem('phone') || '';
      if (o) {
        order30(o);
      } else {
        $('.tips').show();
      }
    });

    $('#login').on('click', function () {
      var phone = $('.phone-num input').val();
      var code = $('.code-num input').val();
      if (!isPhoneNum(phone)) {
        return;
      }
      if (!isCode(code)) {
        return;
      }
      login(phone, code);
    });

    $('#logout').on('click', function () {
      localStorage.clear();
      $('.login').show();
      $('.user').hide();
    });

    $('.more').on('click', function () {
      $('.main').hide();
      $('.note').show();
    });

    $('#closeNote').on('click', function () {
      $('.main').show();
      $('.note').hide();
    });

    $('.get-code').on('click', function () {
      var tel = $('.phone-num input').val();
      if (num < 60) return;
      if (tel && isPhoneNum(tel)) {
        tm && clearInterval(tm);
        getCode(tel);
        num = 60;
        tm = setInterval(function () {
          num--;
          $('#btn-code').html('倒计时' + num + '秒');
          if (num === 0) {
            $('#btn-code').html('获取验证码');
            num = 60;
            clearInterval(tm);
          }
        }, 1000);
      }

    });

  })();
</script>
</body>
</html>
